#!/bin/bash

HARBOR_VERSION="v2.4.3"
HARBOR_PKG="harbor-offline-installer-${HARBOR_VERSION}.tgz"
SRC_DIR="/root"
INSTALL_DIR="/opt/harbor"
HOSTNAME="harbor.local"
HARBOR_PORT="8080"

echo ">>> [1/5] 安装docker依赖并添加源..."
yum install -y yum-utils device-mapper-persistent-data lvm2 tar
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast

#=========================
# 安装 Docker 
#=========================
if ! command -v docker &>/dev/null; then
    echo "[+] 安装 Docker..."
    yum install -y docker-ce docker-ce-cli containerd.io
    systemctl enable --now docker
else
    echo "[+] Docker 已安装"
fi
echo ">>> [1.1/5] 添加镜像加速器..."
mkdir -p /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
 "registry-mirrors": [
    "https://docker.m.daocloud.io",
    "https://hub-mirror.c.163.com",
    "https://mirror.baidubce.com",
    "https://your_preferred_mirror",
    "https://dockerhub.icu",
    "https://docker.registry.cyou",
    "https://docker-cf.registry.cyou",
    "https://dockercf.jsdelivr.fyi",
    "https://docker.jsdelivr.fyi",
    "https://dockertest.jsdelivr.fyi",
    "https://mirror.aliyuncs.com",
    "https://dockerproxy.com",
    "https://mirror.baidubce.com",
    "https://docker.m.daocloud.io",
    "https://docker.nju.edu.cn",
    "https://docker.mirrors.sjtug.sjtu.edu.cn",
    "https://docker.mirrors.ustc.edu.cn",
    "https://mirror.iscas.ac.cn",
    "https://docker.rainbond.cc"
    ]
}
EOF

systemctl daemon-reload
systemctl restart docker
echo ">>> [1.2/5] 验证docker安装..."
docker --version
docker run --rm hello-world || echo "⚠️ hello-world 拉取失败，请检查网络"
echo ">>> [1.3/5] docker已安装成功..."
#==========================
# docker-compose (需提前上传)
#==========================
if ! command -v docker-compose &>/dev/null; then
   if [ -f "${SRC_DIR}/docker-compose" ]; then
    cp -f ${SRC_DIR}/docker-compose /usr/local/bin/docker-compose
    chmod 777 /usr/local/bin/docker-compose
    ln -sf /usr/local/bin/docker-compose /usr/bin/docker-compose
    echo "[+] docker-compose 已就绪"
else
    echo "❌ 未找到 docker-compose，请上传到 ${SRC_DIR}/docker-compose"
    exit 1
fi
else
    echo "[+] docker-compose 已安装"
fi

#==========================
# Harbor 安装包 (需提前上传)
#==========================
if [ ! -f "${SRC_DIR}/${HARBOR_PKG}" ]; then
    echo "❌ 未找到 ${HARBOR_PKG}，请上传到 ${SRC_DIR}/"
    exit 1
fi

echo ">>> [2/5] 解压 Harbor..."
mkdir -p $INSTALL_DIR
tar xf ${SRC_DIR}/${HARBOR_PKG} -C $INSTALL_DIR --strip-components=1

#==========================
# 配置 harbor.yml
#==========================
echo ">>> [3/5] 配置 harbor.yml..."
cd $INSTALL_DIR
cp -f harbor.yml.tmpl harbor.yml
sed -i "s/^hostname:.*/hostname: ${HOSTNAME}/" harbor.yml
sed -i "s/^  port: .*/  port: ${HARBOR_PORT}/" harbor.yml
sed -i "s/^https:.*/#https:/g" harbor.yml
sed -i "s/^  port: 443/#  port: 443/g" harbor.yml
sed -i "s/^  certificate: .*/#  certificate:/g" harbor.yml
sed -i "s/^  private_key: .*/#  private_key:/g" harbor.yml

#==========================
# 安装 Harbor
#==========================
echo ">>> [4/5] 安装 Harbor..."
./install.sh --with-trivy

#==========================
# 启动 Harbor
#==========================
echo ">>> [5/5] 启动 Harbor..."
docker-compose up -d

echo "=================================================="
echo "✅ Harbor ${HARBOR_VERSION} 已安装完成！"
echo "访问地址: http://${HOSTNAME}:${HARBOR_PORT}"
echo "默认账户: admin"
echo "初始密码: Harbor12345"
echo "安装目录: ${INSTALL_DIR}"
echo "=================================================="
